package com.hktf.corelib.utils;

/**
 * Created by cdz
 */
public class UnsignedUtil {

    public static int bytes2Int(byte[] bytes,int startIdex,boolean isBig){
//        ByteArrayInputStream inputStream= new ByteArrayInputStream(bytes);
//        return inputStream.read();
        int firstByte = (0x000000FF & (isBig?(int)bytes[startIdex]:(int)bytes[startIdex+1]));
        int secondByte = (0x000000FF & (isBig?(int)bytes[startIdex+1]:(int)bytes[startIdex]));
        int ToUnsignedInt = ((int)( firstByte << 8 |secondByte)) & 0xFFFFFFFF;

        return ToUnsignedInt;
    }
    /**
     * java byte (1 byte == 8 bit) (-2^7~2^7-1 : -128~127) to unsigned short(0~2^8-1 : 0~255)
     *
     * @param data
     * @return
     */
    public static int getUnsignedByte (byte data){
        return data&0x0FF;
    }

    /**
     * java short (1 short == 2 byte == 16 bit) (-2^15~2^15-1 : -32768~32767) to unsigned short(0~2^16-1 : 0~65535)
     *
     * @param data
     * @return
     */
    public static int getUnsignedShort (short data){
        return data&0x0FFFF;
    }

    /**
     * java int (1 int == 4 byte == 32 bit)(-2^31~2^31-1 : -2147483648~2147483647) to unsigned short(0~2^32-1 : 0~4294967295)
     *
     * @param data
     * @return
     */
    public static long getUnsignedInt (int data){
        return data&0x0FFFFFFFF;
    }

}